package com.java.ratelimter.service;

import com.google.common.util.concurrent.RateLimiter;
import com.java.ratelimter.constant.LimterConstant;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

/**
 * @author : spwei
 * create at:  2019/5/13  下午7:43
 * @description: 限流基础类
 */
@Service
@Slf4j
public class BaseLimterService {


    public Boolean limiter(String key,Integer rate){
        RateLimiter limiter =  LimterConstant.LIMITER_MAP.get(key);
        if(null == limiter) {
            limiter = RateLimiter.create(rate);
            LimterConstant.LIMITER_MAP.put(key, limiter);
        }
        return limiter.tryAcquire();
    }


}
